package com.android.sama;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;

import WebService.ServiceCommunicate;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class searchProduct extends Activity{

	EditText productID;
	Button barcodeBt,getBt;
	ImageView img;
	TextView productName,productCat,productSup,productPrice,unitInStock,unitonOrder,location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.productinfo);
		productID=(EditText)findViewById(R.id.id_product);
		barcodeBt=(Button)findViewById(R.id.barcodeBt);
		img=(ImageView)findViewById(R.id.productImage);
		productCat=(TextView)findViewById(R.id.product_cat);
		productName=(TextView)findViewById(R.id.product_name);
		productSup=(TextView)findViewById(R.id.product_supplier);
		productPrice=(TextView)findViewById(R.id.product_price);
		unitInStock=(TextView)findViewById(R.id.unit_instock);
		unitonOrder=(TextView)findViewById(R.id.unit_onorder);
		location=(TextView)findViewById(R.id.local);

		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		SharedPreferences save=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		final String url=save.getString("SERVER_URL","");
		getBt=(Button)findViewById(R.id.get_btn);
		
	
		
		getBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ServiceCommunicate pd=new ServiceCommunicate();
				if(productID.getText().toString().trim()=="")
				{
					builder.setMessage("Please enter barcode!");
				    AlertDialog alert = builder.create();
					alert.show();
					return;
				}
				SoapObject sob=pd.GetSoapObject(productID.getText().toString(),url);
		
	        	
				try{
				
		       //productID.setText(sob.getProperty("productID").toString());
				productName.setText(sob.getProperty("ProductName").toString());
				productCat.setText(sob.getProperty("CategoryName").toString());
				productSup.setText(sob.getProperty("Company").toString());
				productPrice.setText(sob.getProperty("PricePerUnit").toString());
				unitInStock.setText(sob.getProperty("UnitInstock").toString());
				unitonOrder.setText(sob.getProperty("UnitOnOrder").toString());
				location.setText(sob.getProperty("Location").toString());
				byte[]data=Base64.decode(sob.getProperty("Image").toString(),Base64.DEFAULT);
				Bitmap  bm=BitmapFactory.decodeByteArray(data, 0, data.length);
				img.setImageBitmap(bm);
			
				}
				catch(Exception e)
				{
					builder.setMessage("Product not found!");
				    AlertDialog alert = builder.create();
					alert.show();
					return;
				}
				
			}
		});
		barcodeBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("com.google.zxing.client.android.SCAN");
				intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
				startActivityForResult(intent, 1);
			}
		});
		
		
	}

	@Override
	 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
 	   if (requestCode == 1) {
 	      if (resultCode == RESULT_OK) {
 	        productID.setText( intent.getStringExtra("SCAN_RESULT").toString());
 	         // Handle successful scan
 	    
 	      } else if (resultCode == RESULT_CANCELED) {
 	         // Handle cancel
 	      }
 	   }
 	}
	

}
